package org.one.afternoon.controller;


import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Category;
import org.one.afternoon.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/all")
    public Result<List<Category>> findAll(){
        return Result.success(categoryService.findAll());
    }
    @PostMapping("/queryOne")
    @ResponseBody
    public Result<List<Category>> queryOne(@RequestBody Category category){
        return Result.success(categoryService.queryOne(category));
    }
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<String> insertOne(@RequestBody Category category){
        boolean insert = categoryService.insert(category);
        if (insert){
           return Result.success("添加数据成功");
        }else {
           return Result.error("添加数据失败");
        }
    }
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<String> delete(@RequestParam Integer index){
        boolean resultT=categoryService.deleteById(index);
        if (resultT){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    @PostMapping("/updateOne")
    @ResponseBody
    public Result<String> update(@RequestBody Category category){
        boolean update=categoryService.update(category);
        if (update){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

}
